package org.lq.his.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.shiro.crypto.hash.Md5Hash;
import org.lq.his.dao.DoctorRankClassDao;
import org.lq.his.dao.UserDao;
import org.lq.his.dao.UserOrSectionDao;
import org.lq.his.pojo.Dictionary;
import org.lq.his.pojo.DoctorRankClass;
import org.lq.his.pojo.RegistrationChargeType;
import org.lq.his.pojo.Section;
import org.lq.his.pojo.User;
import org.lq.his.pojo.UserOrSection;
import org.lq.his.service.UserService;
import org.springframework.stereotype.Service;

import com.mysql.fabric.xmlrpc.base.Array;

@Service("userService")
public class UserServiceImpl implements UserService {
	@Resource
	private UserDao userDao;
	
	@Resource
	private DoctorRankClassDao doctorRankClassDao;
	
	
	
	public void setDoctorRankClassDao(DoctorRankClassDao doctorRankClassDao) {
		this.doctorRankClassDao = doctorRankClassDao;
	}


	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}


	@Override
	public User getByUserCode(String userCode) {
		
		return userDao.getByUserCode(userCode);
	}


	@Override
	public List<User> getListByCondition(int start, int limit, User condition, String column, String orderBy) {
		
		return userDao.getListByCondition(start, limit, condition, column, orderBy);
	}


	@Override
	public Integer getCountByCondition(User condition) {
		
		return userDao.getCountByCondition(condition);
	}


	@Override
	public void addUser(User user,Integer[] roleIds) {
		Md5Hash md5Hash = new Md5Hash(user.getPassword(),user.getSalt());
		String pwd = md5Hash.toString();
		user.setPassword(pwd);
		userDao.add(user);
		userDao.addUserOrRole(user.getId(),roleIds);
		userDao.addUserOrSection(user);
	}


	@Override
	public List<User> getAll() {
		return userDao.getAll();
	}


	@Override
	public void bathDeleteUser(Integer[] ids) {
		userDao.deleteByIds(ids);
	}


	@Override
	public User getUserById(Integer id) {
		return userDao.getById(id);
	}


	@Override
	public void edtiUser(User user, Integer[] roleIds) {
		Md5Hash md5Hash = new Md5Hash(user.getPassword(),user.getSalt());
		String pwd = md5Hash.toString();
		user.setPassword(pwd);
		//删除用户和权限关系表
		userDao.deleteUserOrRole(user.getId());
		//删除用户和科室关系表
		userDao.deleteUserOrSection(user.getId());
		//修改用户
		userDao.update(user);
		//添加用户和权限表
		userDao.addUserOrRole(user.getId(), roleIds);
		//添加用户和科室表
		userDao.addUserOrSection(user);
		
	}


	@Override
	public List<User> getShiftDoctorByCondition(Integer registrationChargeTypeId, Integer sectionId) {
		
		//封装参数
		DoctorRankClass condition = new DoctorRankClass();
		//封装挂号类型
		RegistrationChargeType registrationChargeType = new RegistrationChargeType();
		registrationChargeType.setId(registrationChargeTypeId);
		
		condition.setRegistrationChargeType(registrationChargeType);
		//封装科室
		Section section = new Section();
		section.setId(sectionId);
		condition.setSection(section);
		
		//根据科室和挂号类型查询值班医生
		
		List<DoctorRankClass> list =  doctorRankClassDao.getShiftDoctor(condition);
		
		Integer[] ids = new Integer [list.size()];
		for (int i = 0; i < list.size(); i++) {
			ids[i] = list.get(i).getUser().getId();
		}

		List<User> list2 = new ArrayList<>();
		if(ids.length>0) {
			list2 = userDao.getUserByIds(ids);
		}
		
		return list2;
	}

}
